<?php

namespace App\Http\Controllers;

use App\Models\Member;
use App\Models\Red;
use App\Models\Share;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ShareController extends Controller
{
    /**
     * 获取一个分享
     */
    public function show($_share_id)
    {
        $Share = DB::table('shares')
            ->where("id",$_share_id)
            ->first();
        if($Share) {
            $_response = response(['data'=>$Share]);
        } else {
            $_response = response(['data'=>0]);
        }
        return $_response;
    }

    /**
     * 获取分享数据
     */
    public function find($_red_id)
    {
        $Red = Red::where("id",$_red_id)
            ->select("title","member_id","red_type","media")
            ->first();
        $Red->setAppends(['cover']);
        if($Red) {
            $_share = array(
                "title" => $Red->title,
                "desc" => Member::where("id",$Red->member_id)->value("nickname")."的红包",
                "link" => route("app", ["share_id"=>$this->getShareId($_red_id)]),
                "imgUrl" => $Red->cover ? $Red->cover : asset('assets/images/share-cover.jpg')
            );
            $_response = response(['data'=>$_share]);
        } else {
            $_response = response(['message'=>'红包不存在'],402);
        }
        return $_response;
    }
}
